﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Hosting.Class
{
    public class Articles
    {

        private HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();

        public Articles() { }

        #region [Get all articles]
        /// <summary>
        /// Get All articles
        /// </summary>
        /// <returns>
        /// <para>null                                  : There is an error</para>
        /// <para>.ToList().Count greater than 0        : Success</para>
        /// <para>.ToList().Count less-equal than 0     : There are not any data</para>
        /// </returns>
        public List<PC_Articles_GetAllResult> getAll()
        {
            try
            {
                var articles = db.PC_Articles_GetAll().ToList();
                return articles.ToList();
            }
            catch(Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Get one article]
        /// <summary>
        /// Get one article
        /// </summary>
        /// <returns>
        /// <para>null                                  : There is an error</para>
        /// <para>.ToList().Count greater than 0        : Success</para>
        /// <para>.ToList().Count less-equal than 0     : There are not any values</para>
        /// </returns>
        public List<PC_Articles_GetOneResult> getOne(string id)
        {
            try
            {
                var articles = db.PC_Articles_GetOne(Int32.Parse(id)).ToList();
                return articles.ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Get articles by Group Id]
        /// <summary>
        /// Get one by group Id
        /// </summary>
        /// <param name="id">Id of Article Group ID</param>
        /// <param name="getActive">
        /// <para>0     : false</para>
        /// <para>1     : true</para>
        /// <para>-1    : get all</para>
        /// </param>
        /// <returns>
        /// <para>null                                  : There is an error</para>
        /// <para>.ToList().Count greater than 0        : Success</para>
        /// <para>.ToList().Count less-equal than 0     : There are not any data</para>
        /// </returns>
        public List<PC_Articles_GroupIdResult> getByGroupId(string id, string getActive)
        {
            try
            {
                var articles = db.PC_Articles_GroupId(Int32.Parse(id), Int32.Parse(getActive)).ToList();
                return articles.ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Insert a new Article]
        /// <summary>
        /// Insert a new Article
        /// </summary>
        /// <param name="username">Username who writes aritcle</param>
        /// <param name="groupId">ID group contains articles</param>
        /// <param name="title">Title of article</param>
        /// <param name="content">Content of article</param>
        /// <param name="active">Visible/Hidden Article</param>
        /// <returns>
        /// <para>0    : success</para>
        /// <para>-99  : error</para>
        /// </returns>
        public int Add(string username, string groupId, string title, string content, bool active)
        {
            try
            {
                int isAdd = db.PC_Articles_Ins(username, Int32.Parse(groupId), title, content, active);
                return isAdd;
            }
            catch(Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check insert is valid or not]
        /// <summary>
        /// Check insert is valid or not
        /// </summary>
        /// <param name="groupId">Group ID contains articles</param>
        /// <param name="title">Title of article</param>
        /// <returns>
        /// <para>1    :success</para>
        /// <para>2    : Article Group is null or empty</para>
        /// <para>3    : Article Group is not valid</para>
        /// <para>10   : Article title is null or empty</para>
        /// <para>11   : Article Title is less than 6 characters</para>
        /// <para>12   : Article Title is greater than 100</para>
        /// </returns>
        public int AddValid(string groupId, string title)
        {
            if (String.IsNullOrEmpty(groupId))
            {
                return 2;
            }

            Regex rGroupId = new Regex("^[0-9]{0,}$");
            Match mGroupId = rGroupId.Match(groupId);
            if (!mGroupId.Success)
            {
                return 3;
            }
            else if(String.IsNullOrEmpty(title))
            {
                return 10;
            }
            else if (title.Length < 6)
            {
                return 11;
            }
            else if (title.Length > 100)
            {
                return 12;
            }

            return 1;
        }
        #endregion

        #region [Update a new Article]
        /// <summary>
        /// Update a new Article
        /// </summary>
        /// <param name="articleId">Id of Article</param>
        /// <param name="username">Username who writes aritcle</param>
        /// <param name="groupId">ID group contains articles</param>
        /// <param name="title">Title of article</param>
        /// <param name="content">Content of article</param>
        /// <param name="active">Visible/Hidden Article</param>
        /// <returns>
        /// <para>0    : success</para>
        /// <para>-99  : error</para>
        /// </returns>
        public int Edit(string articleId, string username, string groupId, string title, string content, bool active)
        {
            try
            {
                int isEdit = db.PC_Articles_Upd(Int32.Parse(articleId), username, Int32.Parse(groupId), title, content, active);
                return isEdit;
            }
            catch(Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check update is valid or not]
        /// <summary>
        /// Check insert is valid or not
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="title"></param>
        /// <param name="articleId">Id of article</param>
        /// <param name="groupId">ID group contains articles</param>
        /// <param name="title">Title of article</param>
        /// <returns>
        /// <para>1    :success</para>
        /// <para>2    : Article Group is null or empty</para>
        /// <para>3    : Article Group is not valid</para>
        /// <para>4    : Article Group is null or empty</para>
        /// <para>5    : Article Group is not valid</para>
        /// <para>10   : Article title is null or empty</para>
        /// <para>11   : Article Title is less than 6 characters</para>
        /// <para>12   : Article Title is greater than 100</para>
        /// </returns>
        public int EditValid(string articleId, string groupId, string title)
        {
            if (String.IsNullOrEmpty(articleId))
            {
                return 2;
            }
            
            Regex rArtileId = new Regex("^[0-9]{0,}$");
            Match mArtileId = rArtileId.Match(articleId);
            if(!mArtileId.Success)
            {
                return 5;
            }
            else if (String.IsNullOrEmpty(groupId))
            {
                return 3;
            }

            Regex rGroupId = new Regex("^[0-9]{0,}$");
            Match mGroupId = rGroupId.Match(groupId);
            if (!mGroupId.Success)
            {
                return 4;
            }
            else if (String.IsNullOrEmpty(title))
            {
                return 10;
            }
            else if (title.Length < 6)
            {
                return 11;
            }
            else if (title.Length > 100)
            {
                return 12;
            }

                return 1;
        }
        #endregion

        #region [Delete an existed Article]
        /// <summary>
        /// Delete an existed Article
        /// </summary>
        /// <param name="id">ID of Article</param>
        /// <returns>
        /// <para>0     : successfully</para>
        /// <para>-99   : error</para>
        /// </returns>
        public int delete(string id)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isDelete = db.PC_Articles_Del(Int32.Parse(id));
                return isDelete;
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion
    }
}